<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/7
 * Time: 23:42
 */
namespace app\model;

use think\facade\Db;
use think\Model;

class Warehouse extends Model
{

    protected $pk = 'w_id';

    private function _getListField()
    {
        return [
            'w_id' => 'warehouseId',
            's_id' => 'suppliesId',
            's_name' => 'name',
            's_type' => 'type',
            'st_name' => 'typeName',
            's_brand' => 'brand',
            's_spec' => 'spec',
            's_unit' => 'unit',
            'w_stock' => 'stock',
            'w_create_time' => 'createTime',
        ];
    }

    public function getWarehouseInfoById($id,$field = '')
    {
        $field = $field ?: self::_getListField();
        return Db::table('warehouse')
            ->leftJoin('supplies','w_supplies_id=s_id')
            ->leftJoin('supplies_type','st_id=s_type')
            ->leftJoin('user','user.id=w_uid')
            ->leftJoin('organization','organization.id=w_orgid')
            ->where(['w_id'=>$id])->field($field)->find();
    }

    public function getWarehouseInfoByWhere($where,$field = '*')
    {
        $field = $field ?: self::_getListField();
        return Db::table('warehouse')
            ->where($where)->field($field)->find();
    }

    public function addOneWarehouse($data)
    {
        $data['w_create_time'] = time();
        return Db::table('warehouse')->insertGetId($data);
    }

    public function getWarehouseListByCondition($where = [],$order = 's_id desc',$field = '')
    {
        $field = $field ?: self::_getListField();
        $list = Db::table('warehouse')
            ->leftJoin('supplies','w_supplies_id=s_id')
            ->leftJoin('supplies_type','st_id=s_type')
            ->leftJoin('user','user.id=w_uid')
            ->leftJoin('organization','organization.id=w_orgid')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->render();
        return $list;
    }

    public function getWarehouseTypeList($where = [])
    {
        $field = [
            'st_name' => 'typeName',
            'st_id' => 'type'
        ];
        return $this->leftJoin('supplies','s_id=w_supplies_id')
            ->leftJoin('supplies_type','st_id=s_type')
            ->where($where)
            ->field($field)
            ->group('s_type')
            ->select()->toArray() ?? [];
    }

    public function getWarehouseList($where = [])
    {
        $field = [
            's_name' => 'name',
            's_unit' => 'unit',
            's_brand' => 'brand',
            's_spec' => 'spec',
            'w_stock' => 'stock',
            'st_name' => 'typeName',
            'w_id' => 'warehouseId'
        ];
        return Db::table('warehouse')
                ->leftJoin('supplies','s_id=w_supplies_id')
                ->leftJoin('supplies_type','st_id=s_type')
                ->where($where)
                ->field($field)
                ->select()->toArray() ?? [];
    }
}